<!-- 确定按钮带倒计时功能 -->
<template>
  <el-dialog v-model="dialogVisible" width="660">
    <div class="icon">
      <el-image :src="iconPng" mode="aspectFit" />
    </div>
    <div class="txt">
      <div clss="con">创建成功！</div>
      <div clss="con">正在打印单据，请取单据开始拣货。</div>
    </div>
    <div class="btn-group">
      <div class="wh-btn primary" @click="confirm">确定{{ second }}s后关闭</div>
      <div class="wh-btn info" @click="cancel" v-if="props.showCancel">
        取消
      </div>
    </div>
  </el-dialog>
</template>

<script lang="ts" setup>
import { ElDialog, ElImage } from "element-plus";
import { ref, onMounted, render, watch } from "vue";
import iconPng from "@/assets/img/success.png";
// const emit = defineEmits(["confirm", "cancel"]);
const props = defineProps({
  show: {
    type: Boolean,
    default: true
  },
  type: {
    type: String,
    default: "warning"
  },
  // 提示信息
  message: {
    type: String,
    default: ""
  },
  // 时长
  duration: {
    type: Number,
    default: 3000
  },
  // 是否显示取消按钮
  showCancel: {
    type: Boolean,
    default: false
  },
  // 点击确定按钮的回调
  confirmFn: {
    type: Function
  }
});
const dialogVisible = ref(false);
const second = ref(Math.floor(props.duration / 1000));
const timeHandler = ref();

watch(
  () => {
    return props.show;
  },
  value => {
    dialogVisible.value = value;
  },
  {
    immediate: true
  }
);

function confirm() {
  dialogVisible.value = false;
  if (props.confirmFn) {
    props.confirmFn();
  }
}
function cancel() {
  dialogVisible.value = false;
}

onMounted(() => {
  if (timeHandler.value) {
    clearInterval(timeHandler.value);
  }
  timeHandler.value = setInterval(() => {
    if (second.value > 0) {
      second.value = second.value - 1;
    }
  }, 1000);
  setTimeout(() => {
    dialogVisible.value = false;
  }, props.duration);
});
</script>
<style lang="scss" scoped>
.el-dialog {
  .icon {
    margin-top: 30px;
    display: flex;
    justify-content: center;
    width: 100px;
    height: 100px;
    margin: auto;
  }
  .txt {
    margin-top: 30px;
    font-size: 22px;
    font-weight: bold;
    text-align: center;
    letter-spacing: 0em;
    color: #3f4254;
    .con {
      margin-top: 5px;
    }
  }
  .btn-group {
    margin: auto;
    margin-top: 60px;
    display: flex;
    align-items: center;
    justify-content: space-around;
    width: 500px;
    .wh-btn {
      width: 200px;
      height: 50px;
    }
  }
}
</style>
